package com.kude.dao;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Properties;

public class PropertyDAO {
	 
	 /** 
	 25.     * 根据Key 读取Value 
	 26.     *  
	 27.     * @param key 
	 28.     * @return 
	 29.     */ 
	  public String readData(String key, String file) {
		   file = this.getClass().getResource(file).getFile();
	         Properties props = new Properties();
	         try {  
	             InputStream in = new BufferedInputStream(new FileInputStream(  
	                     file));  
	             props.load(in);  
	             in.close();  
	             String value = props.getProperty(key);  
	             return value;  
	         } catch (Exception e) {  
	             e.printStackTrace();  
	             return null;  
	         }  
	     }  
	 
	  
	  public String readData(String file) throws IOException{
		  file = this.getClass().getResource(file).getFile();
		  File f = new File(file);
		  if(!f.exists()){
			  f.createNewFile();
		  }
		  String encode = "utf-8";
		  BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f),encode));
		  StringBuilder result = new StringBuilder();
		  String line = null;
		  while((line = br.readLine()) != null){
			  result.append(line);
		  }
		  br.close();
		  System.out.println(result.toString());
		  return result.toString();
	  }
	       
	    /** 
	 .     * 修改或添加键值对 如果key存在，修改 反之，添加。 
	 48.     *  
	 49.     * @param key 
	 50.     * @param value 
	 51.     
	     * @throws IOException */ 
	    public void writeData(String value, String fileStr) throws IOException {  
	    	fileStr = this.getClass().getResource(fileStr).getFile();
	    	File f = new File(fileStr);
			  if(!f.exists()){
				  f.createNewFile();
			  }
			  System.out.println("value:" + value);
			  Writer bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f,false), "utf-8"));
			  bw.write(value);
			  bw.close();
	    }

	    
	    public void writeData(String key, String value, String fileStr) throws IOException {  
//	    	fileStr = this.getClass().getResource(fileStr).getFile();
	        
	    }
	 /**
	  * @param args
	  */
	 public static void main(String[] args) {
		 

	  // TODO Auto-generated method stub
		PropertyDAO test = new PropertyDAO();
        String file = test.getClass().getResource("/com/teamtwo/base/produce.txt").getFile();
	 
	//  test.writeData("name", "xiaoyang");
	 
//	  String name = test.readData("produce", file);
//	  System.out.println("The name of the person is:" + name);
        
        
        try {
			test.readData(file);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
//        try {
//			test.writeData(value, file);
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	 }
}
